var network = require("../../api/api.js")

function getRandomColor() {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({
  onReady: function (res) {
    // this.videoContext = wx.createVideoContext('video-livePlayer')
    this.videoContext = wx.createVideoContext('myVideo1')
  },
  onLoad: function (options) {
    let query = JSON.parse(options.query || '{}');
    let key = "resource_id_" + query.resource_id;
    var value = wx.getStorageSync(key);
    query.vedio_path = value;
    // query.vedio_path = "http://118.24.50.38:8081/ms/image/getVideo?path=/resource/video/a4080686162e20e08e43afc25337f85d.mp4";
    this.setData({
      item: query
    })
  },
  inputValue: '',
  data: {
    san: true,
    i: 1, //变量
    src: 'http://223.87.182.49/6573B5C0E7F418178D1A8D5888/03000AFF005B4DF10F847B5899BFFD025A829D-7153-2A37-ED5D-FF8D29D58187.mp4?ccode=0519&duration=483&expire=18000&psid=0af052f7ef9c20ee90e4c0908045cbb7&sp=&ups_client_netip=70365b47&ups_ts=1531905150&ups_userid=1484913675&utid=xudGETJNWEgCAdpYFZJPHa9L&vid=XMzcyODg2NjM4OA%3D%3D&vkey=B420734e5d0667e4cf12f1571f313cc89&s=eee69ce3f59a45e5bfaf',
    item: {},
    result: 0,
    disabled: false
  },
  bindInputBlur: function (e) {
    this.inputValue = e.detail.value
  },
  bindButtonTap: function () {
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success: function (res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
  finished_study: function () {
    var that = this
    var userLogin = wx.getStorageSync('user_login') || {}
    const item = this.data.item || {}
    var params = new Object()
    params.business_param = {}
    params.business_param.user_id = String(userLogin.user_id)
    params.business_param.resource_id = String(item.resource_id)
    network.POST({
      params: params,
      url: "finished_resource",
      success: function (res) {
        const result = res.result[0]
        that.setData({
          disabled: true
        });
          if (result.after_test_status) {
              wx.reLaunch({
                  url: '../Success/Success?paper_id=' + String(result.paper_id) + '&test_status=2'
              })
          }
      },
      fail: function () {
        //失败后的逻辑
      },
    })
  },
  go_test: function () {
    var userLogin = wx.getStorageSync('user_login') || {}
    const item = this.data.item || {}
    var params = new Object()
    params.business_param = {}
    params.business_param.user_id = String(userLogin.user_id)
    params.business_param.paper_id = String(item.paper_id)
    network.POST({
      params: params,
      url: "start_test",
      success: function (res) {
        const result = res.result[0]
        wx.setStorageSync('time_limit', result.time_limit || 90);
        wx.navigateTo({
          url: '../lists/lists?paper_id=' + String(item.paper_id) + '&user_paper_id=' + result.user_paper_id
        })
        //拿到解密后的数据，进行代码逻辑

      },
      fail: function () {
        //失败后的逻辑

      },
    })
  }
})